package main.java.com.pzhu.algorithm;

import java.util.Arrays;

public class _012bubbleSort {


    public static int[] bubbleSort(int[] nums) {
        int temp = 0;
        boolean flag;
        for (int j = 0; j < nums.length - 1; j++) {
            flag = false;
            for (int i = 0; i < nums.length - 1; i++) {
                if (nums[i] > nums[i + 1]) {
                    temp = nums[i];
                    nums[i] = nums[i + 1];
                    nums[i + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) break;
        }
        return nums;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(bubbleSort(new int[]{5, 4, 8, 3, 6, 1})));
        System.out.println(Arrays.toString(bubbleSort(new int[]{5, 4, 3, 2, 1})));
        System.out.println(Arrays.toString(bubbleSort(new int[]{4, 2, 5, 2, 3, 1})));
        System.out.println(Arrays.toString(bubbleSort(new int[]{3, -1, 0, -2, 5, 2})));
    }
}
